from app import create_app, db
from app.models import User, BilingualContent, News
from werkzeug.security import generate_password_hash

app = create_app()

print("开始初始化数据库...")
try:
    with app.app_context():
        print("创建数据库表...")
        db.create_all()
        
        # 初始化管理员账户
        print("检查管理员账户...")
        admin_user = User.query.filter_by(username='admin').first()
        if not admin_user:
            print("创建管理员账户...")
            hashed_password = generate_password_hash('admin123')
            admin = User(username='admin', password=hashed_password)
            db.session.add(admin)
            db.session.commit()
            print("✓ 管理员账户创建成功")
        else:
            print("管理员账户已存在，跳过创建")
        
        # 初始化示例数据
        print("初始化示例数据...")
        from datetime import datetime
        
        # 添加首页轮播图示例
        if not BilingualContent.query.first():
            sample_slide = BilingualContent(
                zh_title="欢迎来到日创科技",
                en_title="Welcome to Innovation Tech",
                zh_content="香港领先的科技解决方案提供商",
                en_content="Hong Kong's Leading Technology Solutions Provider",
                category="home_slider",
                images="static/images/slide1.jpg,static/images/slide2.jpg"
            )
            db.session.add(sample_slide)
            db.session.commit()
            print("示例数据初始化完成！")

        # 添加业务范围示例
        if not BilingualContent.query.filter_by(category='service').first():
            services = [
                {
                    'zh_title': 'IT项目开发外包',
                    'en_title': 'IT Project Outsourcing',
                    'zh_content': '专业定制化软件开发服务...',
                    'en_content': 'Professional custom software development...'
                },
                {
                    'zh_title': '政府资助申请',
                    'en_title': 'Government Funding Application',
                    'zh_content': '协助申请香港各类政府资助计划...',
                    'en_content': 'Assist in applying for Hong Kong government funding...'
                }
            ]
            for s in services:
                service = BilingualContent(category='service', **s)
                db.session.add(service)

        # 添加成功案例
        if not BilingualContent.query.filter_by(category='case').first():
            cases = [
                {
                    'zh_title': '旅行社CRM系统',
                    'en_title': 'Travel Agency CRM System',
                    'zh_content': '为某大型旅行社定制开发的客户管理系统...',
                    'images': 'static/images/case1.jpg'
                }
            ]
            for c in cases:
                case = BilingualContent(category='case', **c)
                db.session.add(case)

        # 添加测试资讯
        if not News.query.first():
            from datetime import datetime, timedelta
            for i in range(5):
                news = News(
                    zh_title=f"行业新闻 {i+1}",
                    en_title=f"Industry News {i+1}",
                    zh_content=f"这是第{i+1}篇中文资讯内容...",
                    en_content=f"This is english content for news {i+1}...",
                    publish_date=datetime.now() - timedelta(days=i)
                )
                db.session.add(news)
        
        db.session.commit()
except Exception as e:
    print(f"初始化失败: {str(e)}") 